$(function(){
    "use strict";
    let file = []
    // let edit_file = []
    //加载所有项目名称
    $.ajax({
        url:'http://jk.com/Admin/Project/projectNI',
        type:'get',
        success:function(data){
            if(data.code==1){
                let datas = data.res
                let add_projectHtmls = '<option></option>'
                for(let pro = 0; pro < datas.length; pro++){
                    add_projectHtmls += ` <option value="${datas[pro].id}">${datas[pro].pro_name}</option> `
                }
                $('#add_people [add_name=add_projectName]').html(add_projectHtmls)
                $('#edit_people [add_name=edit_projectName]').html(add_projectHtmls)
            }
        }
    })
    // 当点击直接投入的时候
    $('.directInto_box>p').click(function(){
        $(this).siblings('ul.directInto_list').toggleClass('active')
    })
    //当点击核算确定的时候
    $('#check').on('click','.cancel_btn',function(){
        $('#check').removeClass('active')
    })
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.query_year)
        }else{
            window.query_year = null
        }
    })
    //当点击查看记录时
    $('#table_table').on('click','.look_record_btn',function(e){
        e.preventDefault()
        $(this).siblings('.tip_hiddenBox').addClass('active')
    })
    // 当点击工时记录中确认按钮时
    $('#table_table').on('click','.work_record_box>.btn',function(e){
        e.preventDefault()
        $(this).parent().removeClass('active')
    })
    //全选的功能
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault()
        if( $(this).html() == '添加' ){
            var add_people = $(this).attr('href')
            $(`[lll=${add_people}]`).addClass('active')
        }
        if( $(this).html() == '核算' ){
            var add_people = $(this).attr('href')
            $(add_people).addClass('active')
        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
    })
    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'http://jk.com/Admin/Res/multiDelRes',
                type:'post',
                data:{
                    res_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                    }
                }
            })
        }else{
            $('#delete').removeClass('active')
        }

    })
    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault()
        let edit_box = $(this).attr('href')
        let edit_id = $(this).closest('tr').attr('data-index')
        $.ajax({
            url:'http://jk.com/Admin/Res/editRes',
            type:'get',
            data:{
                id:edit_id
            },
            success:function(data){
                if(data.code==1){
                    let datas = data.data
                    $('#edit_people').find('[add_name=edit_AchievementName]').val(datas.res_name)
                    $('#edit_people').find('[add_name=edit_projectName]').val(datas.pro_id)
                    $('#edit_people').find('[add_name=edit_projectNum]').val(datas.pro_sn)
                    $('#edit_people').find('[add_name=edit_type]').val(datas.res_type)
                    $('#edit_people').find('[add_name=edit_AchievementLevel]').val(datas.res_level)
                    $('#edit_people').find('[add_name=edit_time]').val(datas.res_time)
                    $('#edit_people').find('[add_name=edit_getType]').val(datas.res_get)
                    $('#edit_people').find('[add_name=edit_file]').parent().siblings('span').html(`<span class="baogao_name">${datas.res_fname}</span><span class="baogao_size">${datas.res_fsize}KB</span><i class="iconfont icon-delete delete_logo"></i>`)
                    file = []
                    file.push(datas.base64)
                    file.push(datas.res_fname)
                    file.push(datas.res_fsize)
                    window.sessionStorage['edit_id'] = edit_id
                }
            }
        })
        $(`[lll=${edit_box}]`).addClass('active')
    })

    // 文件的处理
    $('#Application').on('click','.delete_logo',function(e){
        e.stopPropagation();
        e.preventDefault();
        file = []
        $(this).siblings().remove();
        $(this).remove()
    })
    $('.hidden_input').on('change',function(){
        let data = this.files;
        for(var i =0;i<data.length;i++){
            let fileReader = new FileReader();
            fileReader.readAsDataURL(data[i]);
            let size = Math.round(data[i].size/1024);
            let name = data[i].name;
            let spanHTML = `<span class="baogao_name">${name}</span><span class="baogao_size">${size}KB</span><i class="iconfont icon-delete delete_logo"></i>`;
            $(this).parent().siblings('span').html(spanHTML)
            $(this).attr('ValName',name)
            fileReader.onload = function (e){
                let url = this.result;//上传文件的URL
                file = []
                file.push(url);
                file.push(name);
                file.push(size);

            }
        }
    })

    $('#add_people').find('[add_name=add_projectName]').change(function(){
        let pro_id = $(this).val()
        $.ajax({
            url:'http://jk.com/Admin/Project/getProSn',
            type:'get',
            data:{
                pro_id: pro_id
            },
            success:function(data){
                $('#add_people').find('[add_name=add_projectNum]').val(data.pro_sn)
            }
        })
    })
    // 确认按钮点击时
    $('#add_btn').click(function(){
        let add_AchievementName = $(this).closest('[lll]').find('[add_name=add_AchievementName]').val()
        let add_project_id = $(this).closest('[lll]').find('[add_name=add_projectName]').val()
        let add_projectName = $(this).closest('[lll]').find('[add_name=add_projectName] option:selected').text()
        let add_projectNum = $(this).closest('[lll]').find('[add_name=add_projectNum]').val()
        let add_type = $(this).closest('[lll]').find('[add_name=add_type]').val()
        let add_type_name = $(this).closest('[lll]').find('[add_name=add_type] option:selected').text()
        let add_AchievementLevel = $(this).closest('[lll]').find('[add_name=add_AchievementLevel]').val()
        let add_AchievementLevel_name = $(this).closest('[lll]').find('[add_name=add_AchievementLevel] option:selected').text()
        let add_time = $(this).closest('[lll]').find('[add_name=add_time]').val()
        let add_getType = $(this).closest('[lll]').find('[add_name=add_getType]').val()
        let add_getType_name = $(this).closest('[lll]').find('[add_name=add_getType] option:selected').text()

        if(add_AchievementName == '' || add_projectName == '' || add_projectNum == '' || add_type == '' || add_AchievementLevel == '' || add_time == '' || add_getType == '' || file.length<=0){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let that = this
        $.ajax({
            url:'http://jk.com/Admin/Res/addRes',
            type:'post',
            data:{
                res_name:add_AchievementName,
                pro_id:add_project_id,
                res_type:add_type,
                res_level:add_AchievementLevel,
                res_time:add_time,
                res_get:add_getType,
                res_fkey:file[0],
                res_fname:file[1],
                res_fsize:file[2]
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                    <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="AchievementName">${add_AchievementName}</td>
                            <td data-name="projet_name">${add_projectName}</td>
                            <td data-name="projet_num">${add_projectNum}</td>
                            <td data-name="type_name">${add_type_name}</td>
                            <td data-name="AchievementLevel_name">${add_AchievementLevel_name}</td>
                            <td data-name="time">${add_time}</td>
                            <td data-name="getType">${add_getType_name}</td>
                            <td><a href="${data.path}" class="showPDF">${file[1]}</a></td>
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                        </tr>
            `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).closest('[lll]').removeClass('active')
                }
            }
        })

    })
    // 确认并新增按钮点击时
    $('#add_AddNew_btn').click(function(){
        let add_AchievementName = $(this).closest('[lll]').find('[add_name=add_AchievementName]').val()
        let add_project_id = $(this).closest('[lll]').find('[add_name=add_projectName]').val()
        let add_projectName = $(this).closest('[lll]').find('[add_name=add_projectName] option:selected').text()
        let add_projectNum = $(this).closest('[lll]').find('[add_name=add_projectNum]').val()
        let add_type = $(this).closest('[lll]').find('[add_name=add_type]').val()
        let add_type_name = $(this).closest('[lll]').find('[add_name=add_type] option:selected').text()
        let add_AchievementLevel = $(this).closest('[lll]').find('[add_name=add_AchievementLevel]').val()
        let add_AchievementLevel_name = $(this).closest('[lll]').find('[add_name=add_AchievementLevel] option:selected').text()
        let add_time = $(this).closest('[lll]').find('[add_name=add_time]').val()
        let add_getType = $(this).closest('[lll]').find('[add_name=add_getType]').val()
        let add_getType_name = $(this).closest('[lll]').find('[add_name=add_getType] option:selected').text()

        if(add_AchievementName == '' || add_projectName == '' || add_projectNum == '' || add_type == '' || add_AchievementLevel == '' || add_time == '' || add_getType == '' || file.length<=0){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let that = this
        $.ajax({
            url:'http://jk.com/Admin/Res/addRes',
            type:'post',
            data:{
                res_name:add_AchievementName,
                pro_id:add_project_id,
                res_type:add_type,
                res_level:add_AchievementLevel,
                res_time:add_time,
                res_get:add_getType,
                res_fkey:file[0],
                res_fname:file[1],
                res_fsize:file[2]
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                    <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="AchievementName">${add_AchievementName}</td>
                            <td data-name="projet_name">${add_projectName}</td>
                            <td data-name="projet_num">${add_projectNum}</td>
                            <td data-name="type_name">${add_type_name}</td>
                            <td data-name="AchievementLevel_name">${add_AchievementLevel_name}</td>
                            <td data-name="time">${add_time}</td>
                            <td data-name="getType">${add_getType_name}</td>
                            <td><a href="${data.path}" class="showPDF">${file[1]}</a></td>
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                        </tr>
            `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    // $(that).closest('[lll]').removeClass('active')
                }
            }
        })

    })

    $('#edit_people').find('[add_name=edit_projectName]').change(function(){
        let pro_id = $(this).val()
        $.ajax({
            url:'http://jk.com/Admin/Project/getProSn',
            type:'get',
            data:{
                pro_id: pro_id
            },
            success:function(data){
                $('#edit_people').find('[add_name=edit_projectNum]').val(data.pro_sn)
            }
        })
    })
    // 保存按钮点击时
    $('#save_btn').click(function(){
        let edit_AchievementName = $(this).closest('[lll]').find('[add_name=edit_AchievementName]').val()
        let edit_project_id = $(this).closest('[lll]').find('[add_name=edit_projectName]').val()
        let edit_projectName = $(this).closest('[lll]').find('[add_name=edit_projectName] option:selected').text()
        let edit_projectNum = $(this).closest('[lll]').find('[add_name=edit_projectNum]').val()
        let edit_type = $(this).closest('[lll]').find('[add_name=edit_type]').val()
        let edit_type_name = $(this).closest('[lll]').find('[add_name=edit_type] option:selected').text()
        let edit_AchievementLevel = $(this).closest('[lll]').find('[add_name=edit_AchievementLevel]').val()
        let edit_AchievementLevel_name = $(this).closest('[lll]').find('[add_name=edit_AchievementLevel] option:selected').text()
        let edit_time = $(this).closest('[lll]').find('[add_name=edit_time]').val()
        let edit_getType = $(this).closest('[lll]').find('[add_name=edit_getType]').val()
        let edit_getType_name = $(this).closest('[lll]').find('[add_name=edit_getType] option:selected').text()

        if(edit_AchievementName == '' || edit_projectName == '' || edit_projectNum == '' || edit_type == '' || edit_AchievementLevel == '' || edit_time == '' || edit_getType == '' || file.length<=0){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let that = this
        let edit_id = window.sessionStorage['edit_id']
        $.ajax({
            url:'http://jk.com/Admin/Res/editRes',
            type:'post',
            data:{
                id:edit_id,
                res_name:edit_AchievementName,
                pro_id:edit_project_id,
                res_type:edit_type,
                res_level:edit_AchievementLevel,
                res_time:edit_time,
                res_get:edit_getType,
                res_fkey:file[0],
                res_fname:file[1],
                res_fsize:file[2]
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    let edit_html_id = $('#table_table').find(`tr[data-index=${edit_id}]`)
                    let edit_html = `
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="AchievementName">${edit_AchievementName}</td>
                            <td data-name="projet_name">${edit_projectName}</td>
                            <td data-name="projet_num">${edit_projectNum}</td>
                            <td data-name="type_name">${edit_type_name}</td>
                            <td data-name="AchievementLevel_name">${edit_AchievementLevel_name}</td>
                            <td data-name="time">${edit_time}</td>
                            <td data-name="getType">${edit_getType_name}</td>
                            <td><a href="${data.path}" class="showPDF">${file[1]}</a></td>
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                    `
                    edit_html_id.html(edit_html)

                    $(that).parent().parent().removeClass('active')
                }
            }
        })

        // tableHTML.children('[data-name=edit_name]').html(name)
        // tableHTML.children('[data-name=edit_departMent]').html(department)
        // tableHTML.children('[data-name=edit_baseSalary]').html(baseSalary)
        // tableHTML.children('[data-name=edit_bonus]').html(bonus)
        // tableHTML.children('[data-name=edit_Insurance]').html(Insurance)
        // tableHTML.children('[data-name=edit_fund]').html(fund)

    })
    // 取消按钮点击时
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })

    // 当点击其他地方，关闭pdf
    //当点击区域不在列表选择范围内则关闭弹框
        $(document).click(function(e){
            var $this = $(e.target);
            if($this.parent().attr('id') != "PDFBox"){
                $('#PDFBox').removeClass('active');
                $("#PDFBox").html('')
            }
        });
    // 渲染PDF
    $('#table_table').on('click','.showPDF',function(e){
        e.preventDefault()
        e.stopPropagation()
        let purl = $(this).attr('href');//要展示的文件路径
        // console.log(purl)
        // let purl = 'JavaScript高级程序设计（第3版）中文 高清 完整.pdf'
        // 下面代码都是处理IE浏览器的情况
        if (window.ActiveXObject || "ActiveXObject" in window) {
            //判断是否为IE浏览器，"ActiveXObject" in window判断是否为IE11
            //判断是否安装了adobe Reader
            for (x = 2; x < 10; x++) {
                try {
                    oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
                    if (oAcro) {
                        flag = true;
                    }
                } catch (e) {
                    flag = false;
                }
            }
            try {
                oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
                if (oAcro4) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            try {
                oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
                if (oAcro7) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            if (flag) {//支持
                pdfShow(purl);//调用显示的方法

            }else {//不支持
                $("#pdfContent").append("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                location = "http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe";
            }

        }else {
            pdfShow(purl);//调用显示的方法
            $("#PDFBox").addClass('active')
        }
    })
    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

    // 显示pdf
    function pdfShow(url){
        $("#PDFBox").append('<iframe style="height:700px;width:700px;margin:0 auto;overflow: hidden" src="'+url+'"></iframe>');
    }

})